package com.ymatou.iapi.optrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.ymatou.iapi.optrade.parameter.req.OrderBizReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.parameter.vo.BizVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.BargainGroupService;
import com.ymatou.iapi.optrade.service.ChangeOrderBizStatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sharding.db.sharding.model.OrderBizPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 砍价团服务通知交易团状态接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeOrderBizStatus {

	private static OrderBizReq orderBizReq;
	private static ChangeOrderBizStatusCall changeorderbizstatusCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("砍价团服务通知交易团状态接口");
	}

	@Before
	public void caseUp() {
		orderBizReq = new OrderBizReq();
		changeorderbizstatusCall = new ChangeOrderBizStatusCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeorderbizstatusCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyBargainGroup() {
		try {
			for (BizVo biz : orderBizReq.getBizVos()) {

				long orderId = biz.getOrderId();

				OrderBizPo actualOrderBiz = orderShardingWapper
						.selectOrderBizbyOrderId(orderId);

				// 校验订单业务来源表order_biz
				verifyOrderBizDb(biz, actualOrderBiz);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 校验订单业务来源表order_biz
	 * 
	 * @param expectedBiz
	 * @param actualOrderBiz
	 */
	private void verifyOrderBizDb(BizVo expectedBiz, OrderBizPo actualOrderBiz) {

		Logger.comment(String.format("校验订单业务来源表(order_biz) orderId:%s",
				actualOrderBiz.getOrderId()));

		Logger.verifyEquals(expectedBiz.getBizId(), actualOrderBiz.getBizNo(),
				"验证数据库bizNo");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(actualOrderBiz.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(actualOrderBiz.getUpdateTime()) < 2,
				"验证数据库UpdateTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(expectedBiz.getBizType(),
				actualOrderBiz.getBizType(), "验证数据库BizType");
		Logger.verifyEquals(expectedBiz.getBizSubType(),
				actualOrderBiz.getBizSubType(), "验证数据库BizSubType");

		Logger.verifyEquals(Utils.zeroIfNull(expectedBiz.getBizStatus()),
				actualOrderBiz.getBizStatus(), "验证数据库BizStatus");

	}

	/**
	 * 校验订单表状态，单主状态操作日志表
	 */
	private void verifyOrderStatus(Long orderId) {

		long userId = -1l;

		// 订单表(order)
		OrderPo ymtOrders = orderShardingWapper.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

		Logger.verifyEquals(18, ymtOrders.getOrderStatus(), "验证orderStatus");

		// 订单主状态操作日志表(order_status_log)
		OrderStatusLogPo orderStatusLogPo = orderShardingWapper
				.selectOrderStatusLogByOrderId(orderId);

		Logger.comment("验证订单主状态操作日志表(order_status_log)");

		Logger.verifyEquals("砍价团取消订单", orderStatusLogPo.getMemo(), "验证数据库Memo");
		Logger.verifyEquals(18, orderStatusLogPo.getOrderStatus(),
				"验证数据库OrderStatus");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(orderStatusLogPo.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, orderStatusLogPo.getOrderId(),
				"验证数据库OrderId");
		Logger.verifyEquals(userId, orderStatusLogPo.getOperator(),
				"验证数据库Operator");

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_001() {
		Logger.start(true, "砍价团订单支付成功,交易这边不发风控检测,通知砍价团服务订单支付成功.");
		try {

			String bizId = TestDataManager.getRandomNum(9);

			BargainGroupService.openGroup(bizId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_002() {
		Logger.start(true, "砍价团开团订单支付成功后,砍价团服务通知交易开团成功.");
		try {
			Logger.comment("准备前置数据");

			String bizId = TestDataManager.getRandomNum(9);

			TradingServiceResp placeOrderResp = BargainGroupService
					.openGroup(bizId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(1);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));

			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

			verifyBargainGroup();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_003() {
		Logger.start(true, "砍价团参团订单支付成功后,砍价团服务通知交易参团成功.");
		try {
			Logger.comment("准备前置数据");

			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			BargainGroupService.openGroup(bizId);

			// 砍价团参团订单
			TradingServiceResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);

			long orderId = placeOrderResp2.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(2);
			bizVo.setBizSubType(2);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

			verifyBargainGroup();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_004() {
		Logger.start(true, "砍价团开团订单支付成功后,砍价团服务通知交易开团失败.订单取消,取消类型为砍价团取消");
		try {
			Logger.comment("准备前置数据");

			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			TradingServiceResp placeOrderResp = BargainGroupService
					.openGroup(bizId);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(-1);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证Code");

			verifyBargainGroup();

			// 订单主表
			verifyOrderStatus(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_005() {
		Logger.start(true, "砍价团参团订单支付成功后,砍价团服务通知交易参团失败.订单取消,取消类型为砍价团取消");
		try {
			Logger.comment("准备前置数据");
			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			BargainGroupService.openGroup(bizId);

			// 砍价团参团订单
			TradingServiceResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);

			long orderId = placeOrderResp2.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(-2);
			bizVo.setBizSubType(2);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

			verifyBargainGroup();

			// 订单主表
			verifyOrderStatus(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_006() {
		Logger.start(true, "砍价团所有订单支付成功后,砍价团服务通知交易组团成功");
		try {
			Logger.comment("准备前置数据");
			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			long orderId = BargainGroupService.openGroup(bizId).getOrderList()
					.get(0).getOrderId();

			// 砍价团参团订单
			TradingServiceResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);

			long orderId2 = placeOrderResp2.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();
			BizVo bizVo2 = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(3);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			bizVo2.setOrderId(orderId2);
			bizVo2.setBizStatus(3);
			bizVo2.setBizSubType(2);
			bizVo2.setBizType(1);
			bizVo2.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo, bizVo2));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

			verifyBargainGroup();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_007() {
		Logger.start(true, "砍价团所有订单支付成功后,砍价团服务通知交易组团失败,该团所有订单取消");
		try {
			Logger.comment("准备前置数据");
			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			long orderId = BargainGroupService.openGroup(bizId).getOrderList()
					.get(0).getOrderId();

			// 砍价团参团订单
			TradingServiceResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);

			long orderId2 = placeOrderResp2.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();
			BizVo bizVo2 = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(-3);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			bizVo2.setOrderId(orderId2);
			bizVo2.setBizStatus(-3);
			bizVo2.setBizSubType(2);
			bizVo2.setBizType(1);
			bizVo2.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo, bizVo2));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

			verifyBargainGroup();

			// 订单主表

			verifyOrderStatus(orderId);
			verifyOrderStatus(orderId2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	// 手工校验发风控
	public void Tc_ChangeOrderBizStatus_008() {
		Logger.start(true, "砍价团服务通知交易组团成功后,组团成功的订单统一发风控");
		try {
			Logger.comment("准备前置数据");
			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			long orderId = BargainGroupService.openGroup(bizId).getOrderList()
					.get(0).getOrderId();

			// 砍价团参团订单
			TradingServiceResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);

			long orderId2 = placeOrderResp2.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();
			BizVo bizVo2 = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(3);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			bizVo2.setOrderId(orderId2);
			bizVo2.setBizStatus(3);
			bizVo2.setBizSubType(2);
			bizVo2.setBizType(1);
			bizVo2.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo, bizVo2));

			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

			verifyBargainGroup();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_009() {
		Logger.start(true, "砍价团服务重复通知交易系统消息");
		try {
			Logger.comment("准备前置数据");
			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			long orderId = BargainGroupService.openGroup(bizId).getOrderList()
					.get(0).getOrderId();

			// 砍价团参团订单
			TradingServiceResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);

			long orderId2 = placeOrderResp2.getOrderList().get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();
			BizVo bizVo2 = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(3);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			bizVo2.setOrderId(orderId2);
			bizVo2.setBizStatus(3);
			bizVo2.setBizSubType(2);
			bizVo2.setBizType(1);
			bizVo2.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo, bizVo2));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.comment("砍价团服务重复通知交易系统消息");

			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

			verifyBargainGroup();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_010() {
		Logger.start(true, "砍价团服务先通知交易开团失败,后通知交易开团成功.");
		try {
			Logger.comment("准备前置数据");

			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			long orderId = BargainGroupService.openGroup(bizId).getOrderList()
					.get(0).getOrderId();

			Logger.comment("准备前置数据结束");

			BizVo bizVo = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(-1);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			bizVo.setBizStatus(1);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

			verifyOrderStatus(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_011() {
		Logger.start(true, "BizVos 为null");
		try {

			orderBizReq.setBizVos(null);
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("fail",
					changeorderbizstatusCall.getReturnData(), "验证返回报文");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_012() {
		Logger.start(true, "BizVos 为null");
		try {

			orderBizReq.setBizVos(null);
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("fail",
					changeorderbizstatusCall.getReturnData(), "验证返回报文");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_013() {
		Logger.start(true, "BizVos 里orderid 为null");
		try {

			BizVo bizVo = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			// bizVo.setOrderId();
			bizVo.setBizStatus(-1);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId("1111");

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ChangeOrderBizStatus_014() {
		Logger.start(true, "BizVos 里bizId 与砍价团id不一样");
		try {
			String bizId = TestDataManager.getRandomNum(9);

			// 开团订单
			long orderId = BargainGroupService.openGroup(bizId).getOrderList()
					.get(0).getOrderId();

			BizVo bizVo = new BizVo();

			// 砍价团(1 开团, 2 参团)

			// 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)

			bizVo.setOrderId(orderId);
			bizVo.setBizStatus(-1);
			bizVo.setBizSubType(1);
			bizVo.setBizType(1);
			bizVo.setBizId("1111");

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));
			changeorderbizstatusCall.setData(orderBizReq);
			changeorderbizstatusCall.callService();

			Logger.verifyEquals("ok", changeorderbizstatusCall.getReturnData(),
					"验证返回报文");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
